package com.weihua.flow;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HDFSApi {
    private FileSystem fs = null;

    //1、初始化方法:生成FileSystem的对象
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        fs = FileSystem.get(new URI("hdfs://192.40.10.130:9000"), new Configuration(), "liushan");
    }

    //3、测试api的方法
    //(1)在hdfs中新建目录/fuxi/wc/input
    @Test
    public void makeDir() throws IOException {
        fs.mkdirs(new Path("/fuxi/wc/input"));
    }

    @Test
    //(2)上传文件word.txt到/fuxi/wc/input
    public void upLoadFile() throws IOException {
        fs.copyFromLocalFile(false, true,
                new Path("D:\\word.txt"),
                new Path("/fuxi/wc/input"));
    }

    //(3)删除已上传的/fuxi/wc/input/word.txt
    @Test
    public void delFile() throws IOException {
        fs.delete(new Path("/fuxi/wc/input/word.txt"), true);
    }

    //(4)删除非空目录
    @Test
    public void delDir() throws IOException {
        fs.delete(new Path("fuxi/wc/input"), true);
    }

    //(5)移动目录:/fuxi/wc/input --> /mvDir
    @Test
    public void mvFile() throws IOException {
        fs.rename(new Path("/fuxi/wc"), new Path("/fuxi/mvDir"));
    }

    //2、关闭连接、释放资源
    @After
    public void close() throws IOException {
        fs.close();
    }
}
